<?php

namespace App\Admin\Extensions\Form\Config;

use Encore\Admin\Facades\Admin;

class Pouch extends ConfigBase
{
    /**
     * The form title.
     *
     * @var string
     */
    public $title = '打卡设置';
    public $incType = 'pouch';

    /**
     * Build a form here.
     */
    public function form()
    {

        $script = <<<SCRIPT

        document.onkeydown = function (event) {
            var eve = event ? event : (window.event ? window.event : null);
            if (eve.keyCode == 13) {
                return false;
            }
        };

                var lat = $('#latitude');
                var lng = $('#longitude');
                var center = new qq.maps.LatLng(lat.val(), lng.val());
                var container = document.getElementById("map_latitudelongitude");
                var map = new qq.maps.Map(container, {
                    center: center,
                    zoom: 16
                });
                var cirle = new qq.maps.Circle({
                    center: center,
                    radius: 300,
                    strokeWeight:1,
                    map: map
                });

                function init(name) {

                var marker = new qq.maps.Marker({
                    position: center,
                    draggable: true,
                    map: map
                });

                if( ! lat.val() || ! lng.val()) {
                    var citylocation = new qq.maps.CityService({
                        complete : function(result){
                        map.setCenter(result.detail.latLng);
                        marker.setPosition(result.detail.latLng);
                    }
                    });

                    citylocation.searchLocalCity();
                }

                qq.maps.event.addListener(map, 'click', function(event) {
                    marker.setPosition(event.latLng);
                     var aa = parseInt($('#pouch_distance').val());
                     cirle.setCenter(new qq.maps.LatLng(event.latLng.lat,event.latLng.lng))
                     cirle.setRadius(aa)
                });

                qq.maps.event.addListener(marker, 'position_changed', function(event) {
                    var position = marker.getPosition();
                    lat.val(position.getLat());
                    lng.val(position.getLng());
                });

                var ap = new qq.maps.place.Autocomplete(document.getElementById("search-latitudelongitude"));

                var searchService = new qq.maps.SearchService({
                    map : map
                });

                qq.maps.event.addListener(ap, "confirm", function(res){
                    searchService.search(res.value);
                });
            }

            init('latitudelongitude');
            $('#pouch_distance').blur(function(){
                var aa = parseInt($(this).val());
                cirle.setRadius(aa);
                if(aa>=300){
                    map.setZoom(15)
                }
            })

SCRIPT;
        Admin::script($script,true);
        $this->latlong('latitude', 'longitude', '打卡地点')->height(600);
        $this->number('pouch_distance','打卡范围(米)')->default(100)->min(0);
        $this->disableReset();
    }
}
